<?php

namespace app\admin\controller\article;

use app\common\controller\Backend;
use app\admin\model\category;
/**
 * 测试管理
 *
 * @icon fa fa-circle-o
 */
class Article extends Backend
{

    /**
     * Article模型对象
     * @var \app\admin\model\Article
     */
    protected $model = null;
    protected $searchFields = 'id,title';
    protected $relationSearch = true;
    protected $noNeedRight = ['dashNotice'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Article;
        $this->view->assign("weekList", $this->model->getWeekList());
        $this->view->assign("flagList", $this->model->getFlagList());
        $this->view->assign("genderdataList", $this->model->getGenderdataList());
        $this->view->assign("hobbydataList", $this->model->getHobbydataList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("stateList", $this->model->getStateList());
        $this->view->assign("categoryList", $this->model->getcategoryList());

    }

    public function import()
    {
        parent::import();
    }

    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with('category')
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('category')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

     //分类
     public function categorydate(){
         $where['type']='article';
        $list = db("category")->field('name,id')->where($where)->select();
        if ($list){
            $arr = [];
            foreach($list as $k => $v){
                $arr[$v['id']] = $v['name'];
            }
            $json = json($arr);
        }
        return $json;
    }

     //通过
     public function pass($ids){

        $row = $this->model->get($ids);
        if(!$row){
            $this->error('该记录不存在');
        }
        $row->ztsh= 1;

        $row->save();
        $this->success();
    }

    //驳回

    public function rebut($ids=null){

         if ($this->request->isPost()) {
             $bhid=$this->request->post("id");
             $bhcontent=$this->request->post("remarks");

            $row = $this->model->get($bhid);
             if(!$row){
              $this->error('该记录不存在');
            }
            $row->ztsh     = 2;
            $row->status = "hidden";
            $row->rebut = $bhcontent;
            $row->save();
            $this->success("操作成功");

        }

        $this->assign('ids',$ids);
        return $this->fetch();
    }






     //重新提交审核
     public function resubmit($ids){
        $row = $this->model->get($ids);
        if(!$row){
            $this->error('该记录不存在');
        }
        $row->ztsh     = 0;
        $row->save();
        $this->success();
    }

    //首页获取通知
    public function dashNotice() {
        $info = $this->model->getInfo(14);
        return json(["code"=>1,"msg"=>"获取成功","data"=>$info]);
    }

}
